home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / DLLCust_Files / PREPOST / AVERAGE.C next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  2.7 KB  |  88 lines

  1. // Dynamic link library implementation of a running average processor
  2.  
  3. #include "NSDLL.h" 
  4.  
  5. #define    buffer(i, j)    bufferData->data[j+(i)*bufferData->length]
  6.  
  7. typedef struct {
  8.     int length;
  9.     NSFloat *data;
  10. } AverageData;
  11.  
  12. /***************************/
  13. /* Activation of component */
  14. __declspec(dllexport) BOOL performPrePost(
  15.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  16.     NSFloat    *input,     // Pointer to the input data
  17.     NSFloat    *output,     // Pointer to the output data
  18.     int rows,         // Number of rows of data
  19.     int cols,         // Number of cols of data
  20.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  21.     )
  22. {
  23.     int i, j;
  24.     int N = getIntParameter(instance, 2, 1);
  25.     AverageData *bufferData = getUserData(instance);
  26.     NSFloat result;
  27.  
  28.     for (i=N-1; i>0; i--)
  29.         for (j=0; j<bufferData->length; j++) 
  30.             buffer(i,j) = buffer(i-1,j);
  31.     for (j=0; j<bufferData->length; j++)
  32.         buffer(0,j) = input[j];
  33.     if (!preprocessor)
  34.         for (j=0; j<bufferData->length; j++)
  35.             output[j] = 0.0f;
  36.     for (j=0; j<bufferData->length; j++) {
  37.         result = 0.0f;
  38.         for (i=0; i<N; i++)
  39.             result += buffer(i,j);
  40.         output[j] += result/N;
  41.     }
  42.     return TRUE;
  43. }
  44.  
  45. /******************************************/
  46. /* Called every time the network is reset */
  47. __declspec(dllexport) void networkReset(
  48.     DLLData    *instance    // Pointer to instance data (may be NULL) 
  49.     )
  50. {
  51.     int i, j;
  52.     int N = getIntParameter(instance, 2, 1);
  53.     AverageData *bufferData = getUserData(instance);
  54.  
  55.     for (i=0; i<N; i++)
  56.         for (j=0; j<bufferData->length; j++) 
  57.             buffer(i,j) = 0.0f;
  58. }
  59.  
  60. /******************************************/
  61. /* Management of instance data (OPTIONAL) */
  62. __declspec(dllexport) DLLData *allocPrePost(
  63.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  64.     int *rows,         // Number of rows of output data, can be changed to reflect a diffenent number for the input data
  65.     int *cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  66.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  67.     )
  68. {
  69.     DLLData *instance = allocDLLInstance(oldInstance);
  70.     AverageData *bufferData = calloc(1,sizeof(AverageData));
  71.     setParameterName(instance, 2, 1, "N", TRUE);
  72.     setIntParameter(instance, 2, 1, 4, FALSE);
  73.     bufferData->length = *rows * *cols;
  74.     bufferData->data = calloc(bufferData->length*getIntParameter(instance, 2, 1), sizeof(NSFloat));
  75.     setUserData(instance, bufferData);
  76.     return instance;
  77. }
  78.  
  79. __declspec(dllexport) void freePrePost(DLLData *instance)
  80. {
  81.     AverageData *bufferData = getUserData(instance);
  82.     if (bufferData) {
  83.         free(bufferData->data);
  84.         free(bufferData);
  85.     }
  86.     freeDLLInstance(instance); 
  87. }
  88.